//
//  FFMacro.h
//  Common
//
//  Created by Damon on 15/4/6.
//  Copyright (c) 2016年 FashonFinger. All rights reserved.
//

#if TARGET_IPHONE_SIMULATOR
#define SIMULATOR 1
#elif TARGET_OS_IPHONE
#define SIMULATOR 0
#endif

#define FFPAGE_SIZE 20

/*!
 *  转换16进制颜色为UIColor对象
 *
 *  @param rgbValue RGB色值，如(0xff0000)
 *
 *  @return UIColor对象
 */
#define UIColorFromHexadecimalRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define UIColorFromRGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

//字体颜色部分
#define UICOLOR_RED UIColorFromHexadecimalRGB(0xff4545)
#define UICOLOR_FONT_YELLOW UIColorFromHexadecimalRGB(0xff4545)
#define UICOLOR_FONT_IMPORTANT_RED UIColorFromHexadecimalRGB(0xf03468)
#define UICOLOR_FONT_IMPORTANT_BLACK UIColorFromHexadecimalRGB(0x333333)
#define UICOLOR_FONT_GENERAL_GRAY UIColorFromHexadecimalRGB(0x666666)
#define UICOLOR_FONT_GENERAL_GRAYLIGHT UIColorFromHexadecimalRGB(0x999999)
#define UICOLOR_FONT_WEAK_GRAYLIGHT UIColorFromHexadecimalRGB(0xc0c0c0)
//背景色部分
#define UICOLOR_BACKGROUND_GRAYLIGHT UIColorFromHexadecimalRGB(0xf0efed)
#define UICOLOR_BACKGROUND_GRAYWHITE UIColorFromHexadecimalRGB(0xffffff)
#define UICOLOR_BACKGROUND_LINE UIColorFromHexadecimalRGB(0xdddddd)

#define UICOLOR_BACKGROUND_HILIGHT_GRAY UIColorFromHexadecimalRGB(0xececec)
//cell分割线

#define COMMON_BLUE UIColorFromHexadecimalRGB(0x61a2d7)


#define UICOLOR_999 UIColorFromHexadecimalRGB(0x999999)
#define UICOLOR_666 UIColorFromHexadecimalRGB(0x666666)
#define UICOLOR_fff UIColorFromHexadecimalRGB(0xffffff)

#define UICOLOR_BACKGROUND UIColorFromHexadecimalRGB(0xf5f5f5)
#define UICOLOR_BACKGROUND_GRAY UIColorFromHexadecimalRGB(0xf5f5f5)
#define CELL_SELECT_COLOR UIColorFromHexadecimalRGB(0xfbfbfb)
//取消 返回字体颜色
#define TITLE_GRAY UIColorFromHexadecimalRGB(0xa3a3a4)
//生鲜APP主体视图颜色：暗绿色
#define RGB_VIEW_GREEN RGB_COLOR(101,188,49)//RGB 暗绿色
#define UICOLOR_FRESH_BODY_GREEN UIColorFromHexadecimalRGB(0x65bc31)//16进制颜色暗绿色
//生鲜APP主体字体颜色：暗绿色
#define RGB_FONT_GREEN RGB_COLOR(101,188,49)
//生鲜APP数字 价格 颜色：橙色
#define RGB_NORMAL_ORANGE RGB_COLOR(255,90,0)  //RGB 橙色
#define UICOLOR_FRESH_NUMBER_ORANGE UIColorFromHexadecimalRGB(0xff5a00)//16进制颜色橙色

#define THEME_IMAGE(imageName) [UIImage imageNamed:(imageName)]

#define RGB_FONT_GRAYLIGHT RGB_COLOR(153,153,153)   //999999
#define RGB_FONT_GRAYMIDDLE RGB_COLOR(102,102,102)  //666666
#define RGB_FONT_GRAY RGB_COLOR(51,51,51)           //333333
#define RGB_VIEW_BG_GRAY UIColorFromHexadecimalRGB(0xf5f5f5)

#define RGB_RED_COLOR UIColorFromHexadecimalRGB(0xff4545)
#define RGB_BLACK_COLOR UIColorFromHexadecimalRGB(0x000000)
#define RGB_GRAY_COLOR UIColorFromHexadecimalRGB(0x666666)
#define RGB_LIGHT_GRAY_COLOR UIColorFromHexadecimalRGB(0x999999)
#define RGB_CELL_GRAY UIColorFromHexadecimalRGB(0xdddddd)
#define RGB_WHITE_COLOR UIColorFromHexadecimalRGB(0xffffff)

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

#define IconFontWithSize(fontSize) [UIFont fontWithName:@"iconfont" size:(fontSize)]
#define IconFontString(string) [NSString stringWithFormat:@"\\U0000%@",string]
//字体
/**
 *  不加粗的字体
 *
 *  @param fsize 字体大小
 *
 *  @return nil
 */
#define APP_FONT(fsize) [UIFont systemFontOfSize:fsize]
//#define APP_FONT_BLOD(fsize) [UIFont systemFontOfSize:fsize]

/**
 *  加粗字体
 *
 *  @param fsize 字体大小
 *
 *  @return nil
 */
#define APP_FONT_PINGFANG_SC(fsize) isIOS9?[UIFont fontWithName:@"PingFangSC-Regular" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_PINGFANG_THIN(fsize) isIOS9?[UIFont fontWithName:@"PingFangSC-Thin" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_PINGFANG_LIGHT(fsize) isIOS9?[UIFont fontWithName:@"PingFangSC-Light" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_PINGFANG_SC_BOLD(fsize) isIOS9?[UIFont fontWithName:@"PingFangSC-Medium" size:fsize]:[UIFont boldSystemFontOfSize:fsize]

#define APP_FONT_HMP(fsize) [UIFont fontWithName:@"HiraMinProN-W6" size:fsize]

///////////////
#define APP_FONT_FUTURASTD_BOLD(fsize) isIOS9?[UIFont fontWithName:@"FuturaStd-Bold" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_FUTURASTD_BOOK(fsize) isIOS9?[UIFont fontWithName:@"FuturaStd-Book" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_FUTURASTD_Heavy(fsize) isIOS9?[UIFont fontWithName:@"FuturaStd-Heavy" size:fsize]:[UIFont systemFontOfSize:fsize]

#define APP_FONT_FUTURASTD_Light(fsize) isIOS9?[UIFont fontWithName:@"FuturaStd-Light" size:fsize]:[UIFont boldSystemFontOfSize:fsize]

#define APP_FONT_FUTURASTD_Medium(fsize) isIOS9?[UIFont fontWithName:@"FuturaStd-Medium" size:fsize]:[UIFont boldSystemFontOfSize:fsize]

//Font Family: Arial
//Font: ArialMT
//Font: Arial-BoldMT
//Font: Arial-BoldItalicMT
//Font: Arial-ItalicMT
#define APP_FONT_ARIAL_BOLD(fsize) [UIFont fontWithName:@"Arial-BoldMT" size:fsize]

#define APP_FONT_SHANGYA_REGULAR(fsize) [UIFont fontWithName:@"RTWSShangYaG0v1-Regular" size:fsize]
#define APP_FONT_BLOD(fsize) [UIFont boldSystemFontOfSize:fsize]
#define APP_TITILE_FONT(fsize) [UIFont systemFontOfSize:fsize]
#define APP_CharterITC_FONT(fsize) [UIFont fontWithName:@"CharterITC-Bold" size:fsize]
#define APP_HelveticaLight_FONT(fsize) [UIFont fontWithName:@"Helvetica-Light" size:fsize]
#define APP_DIDOT_FONT(fsize) [UIFont fontWithName:@"Didot-Bold" size:fsize]
#define APP_DIDOT_NOBOLD_FONT(fsize) [UIFont fontWithName:@"Didot" size:fsize]
#define APP_TIME_FONT(fsize) [UIFont fontWithName:@"DS-Digital" size:fsize]
#define APP_BAG_FONT(fsize) [UIFont fontWithName:@"AkzidenzGroteskBE-LightCn" size:fsize]

//默认加载图
#define DefaultImage_120x120 nil //[UIImage imageNamed:@"120x120"]
#define DefaultImage_160x160 nil //[UIImage imageNamed:@"160x160"]
#define DefaultImage_174x230 nil //[UIImage imageNamed:@"174x230"]
#define DefaultImage_210x210 nil //[UIImage imageNamed:@"210x210"]
#define DefaultImage_250x250 nil //[UIImage imageNamed:@"250x250"]
#define DefaultImage_370x200 nil //[UIImage imageNamed:@"370x200"]
#define DefaultImage_750x370 nil //[UIImage imageNamed:@"750x370"]
#define DefaultImage_750x500 nil //[UIImage imageNamed:@"750x500"]
#define DefaultImage_800x800 nil //[UIImage imageNamed:@"800x800"]

///判断是否运行在模拟器中
//#define isSimulator (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location)
//
//#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#define IOS_VERSION [[[UIDevice currentDevice]systemVersion] floatValue]

///判断iOS系统版本
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define isIOS11 [[[UIDevice currentDevice]systemVersion] floatValue] >= 11.0
#define isIOS10 [[[UIDevice currentDevice]systemVersion] floatValue] >= 10.0
#define isIOS9 [[[UIDevice currentDevice]systemVersion] floatValue] >= 9.0
#define isIOS8 [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0
#define isIOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0
#define isIOS4 [[[UIDevice currentDevice]systemVersion] floatValue] < 5.0

//适配
#define FIT_FLOAT(num) num*IPHONE_WIDTH/375

#define Navigation_Height (isDeviceIphoneX?88:64)
#define TabBar_Height (isDeviceIphoneX?83:49)
#define TabBarBottom_Height (isDeviceIphoneX?34:0)
#define Status_Height (isDeviceIphoneX?44:20)


//获取view的安全范围
//VIEWSAFEAREAINSETS(view).left
//VIEWSAFEAREAINSETS(self.view).right
#define VIEWSAFEAREAINSETS(view) ({UIEdgeInsets i; if(@available(iOS 11.0, *)) {i = view.safeAreaInsets;} else {i = UIEdgeInsetsZero;} i;})

//判断设备
#define isDeviceIphone5 CGSizeEqualToSize([UIScreen mainScreen].currentMode.size, CGSizeMake(640, 1136))
#define isDeviceIphone4 CGSizeEqualToSize([UIScreen mainScreen].currentMode.size, CGSizeMake(640, 960))

#define isDeviceIphoneX CGSizeEqualToSize([UIScreen mainScreen].currentMode.size, CGSizeMake(1125, 2436))

#define KEYWindow ([UIApplication sharedApplication].keyWindow)

#define TICK_START   NSDate *tickTime = [NSDate date];
#define TICK_END   NSLog(@"Time: %f", -[tickTime timeIntervalSinceNow]);

#define IPHONE_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define IPHONE_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define CCP(x,y) CGPointMake(x,y)
#define CCS(w,h) CGSizeMake(w,h)
#define CCR(x,y,w,h) CGRectMake(x,y,w,h)

#define FRAMEX(v) (v.frame.origin.x)
#define FRAMEY(v) (v.frame.origin.y)
#define FRAMEW(v) (v.frame.size.width)
#define FRAMEH(v) (v.frame.size.height)
#define XRIGHT(v) (v.frame.origin.x + v.frame.size.width)
#define YBOTTOM(v) (v.frame.origin.y + v.frame.size.height)

#define TRANS_X(__x) (((__x)/375.0)*IPHONE_WIDTH)
#define TRANS_Y(__y) (((__y)/667.0)*IPHONE_HEIGHT)
#define TRANS_W(__w) (((__w)/375.0)*IPHONE_WIDTH)
#define TRANS_H(__h) (((__h)/667.0)*IPHONE_HEIGHT)

#define RGB_COLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RGBA_COLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define DEBUG_COLOR [FFTools randomColor]  //[FFTools randomColor] [UIColor clearColor]


#define isiPadClient UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

///获取居中的CGRect
#define CGRectCenter(superRect,subRect) \
CGRectMake((superRect.size.width-subRect.size.width)/2, (superRect.size.height-subRect.size.height)/2,  subRect.size.width, subRect.size.height)

///设置视图frame的x
#define CGRectSetX(viewRect,x) \
CGRectMake(x, view.origin.y, view.size.width, view.size.height)

///设置视图frame的y
#define CGRectSetY(viewRect,y) \
CGRectMake(viewRect.origin.x, y, viewRect.size.width, viewRect.size.height)
//获取视图的高
#define GETHEIGHT(view) CGRectGetHeight(view.frame)
//获取视图的宽
#define GETWIDTH(view) CGRectGetWidth(view.frame)
//获取视图的X坐标
#define GETORIGIN_X(view) view.frame.origin.x
//获取视图的Y坐标
#define GETORIGIN_Y(view) view.frame.origin.y
//获取下一个视图的X坐标
#define GETRIGHTORIGIN_X(view) view.frame.origin.x + CGRectGetWidth(view.frame)
//获取下一个视图的Y坐标
#define GETBOTTOMORIGIN_Y(view) view.frame.origin.y + CGRectGetHeight(view.frame)
//拉伸图片
#define _ResizeImage(image)  ([image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height / 2, image.size.width / 2, image.size.height / 2 - 1, image.size.width / 2 - 1)])

#define  SessionIsTemporaryLogin    (([[FFSession sharedInstance] passport] != nil) && ([[[FFSession sharedInstance] passport] lifeType]==0))
#define  SessionIsPermanentLogin    (([[FFSession sharedInstance] passport] != nil) && ([[[FFSession sharedInstance] passport] lifeType]>0))
#define  SessionIsNotLogin          [[FFSession sharedInstance] passport] == nil

#define IsAvailablePhoneNumber(phoneNumber) [[NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^1[0-9]{10}$"] evaluateWithObject:phoneNumber]

#define availablePhoneNumber(phoneNumber) phoneNumber.length >= 6 && phoneNumber.length <= 11

#define ARRAY_OBJ_AT(index) My_objectAtIndex:(index) File:__FILE__ Line:__LINE__
#define ARRAY_ADD_OBJ(obj) My_addObject:(obj) File:__FILE__ Line:__LINE__
#define ARRAY_ADD_OBJS_FROM_ARRAY(objs) My_addObjectsFromArray:(objs) File:__FILE__ Line:__LINE__
#define ARRAY_INSERT_OBJ_AT(obj,index)My_insertObject:(obj) atIndex:index File:__FILE__ Line:__LINE__
#define ARRAY_REMOVEOBJ_AT(index) My_removeObjectAtIndex:(index) File:__FILE__ Line:__LINE__
#define ARRAY_REMOVE_ALLOBJ My_removeAllObjects_File:__FILE__ Line:__LINE__
#define ARRAY_REMOVE_OBJ(obj) My_removeObject:(obj) File:__FILE__ Line:__LINE__
#define ARRAY_JOIN_BY(sep) MY_joinByString:(sep) File:__FILE__ Line:__LINE__

#define DICT_SET_VK(value,key) My_setObject:value forKey:key File:__FILE__ Line:__LINE__
#define DICT_SET_DICT(dict) My_setDict:dict File:__FILE__ Line:__LINE__
#define DICT_HAS_K(key) My_hasKey:key File:__FILE__ Line:__LINE__
#define DICT_REMOVEOBJ_K(key) My_removeObjectForKey:key File:__FILE__ Line:__LINE__
#define DICT_REMOVEOBJS_KS(keys) My_removeObjectsForKeys:keys File:__FILE__ Line:__LINE__
#define DICT_OBJ_FOR_K(key) My_objectForKey:(key) File:__FILE__ Line:__LINE__
#define DICT_ALL_KS My_allKeys_File:__FILE__ Line:__LINE__
#define DICT_ALL_VS My_allValues_File:__FILE__ Line:__LINE__
#define DICT_REMOVE_ALLOBJ My_removeAllObjects_File:__FILE__ Line:__LINE__

#define COUNT My_count_File:__FILE__ Line:__LINE__


#define GETRANDUUID(result) {\
CFUUIDRef uuid_ref = CFUUIDCreate(NULL);\
CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);\
NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];\
CFRelease(uuid_ref);\
CFRelease(uuid_string_ref);\
result = [uuid lowercaseString];\
result = [result stringByReplacingOccurrencesOfString:@"-" withString:@""];\
}\

#define HANDLE_PRICE(price){\
NSString *str = [price substringWithRange:NSMakeRange(price.length%3, price.length-price.length%3)];\
NSString *strs = [price substringWithRange:NSMakeRange(0, price.length%3)];\
for (int i =0; i < str.length; i =i+3) {\
NSString *sss = [str substringWithRange:NSMakeRange(i, 3)];\
price = [strs stringByAppendingString:[NSString stringWithFormat:@",%@",sss]];\
}\
if ([[strs substringWithRange:NSMakeRange(0, 1)] isEqualToString:@","]) {\
price = [strs substringWithRange:NSMakeRange(1, strs.length-1)];\
}\
}\

///声明为单例类
///需要在@interface中声明以下函数原型
///+ (className *)sharedInstance
#define DECLARE_SINGLETON_B(className) \
static className *singletonInstance = nil; \
\
+ (className *)sharedInstance { \
@synchronized (self) { \
if (!singletonInstance) { \
[[self alloc] init]; \
} \
} \
return singletonInstance; \
} \
\
+ (id)allocWithZone:(NSZone *)zone { \
@synchronized (self) { \
if (!singletonInstance) { \
singletonInstance = [super allocWithZone:zone]; \
return singletonInstance; \
} \
} \
return nil; \
} \
\

#define DECLARE_SINGLETON_A(className) \
static className *singletonInstance = nil; \
+ (className *)sharedInstance { \
@synchronized (self) { \
if (!singletonInstance) { \
singletonInstance = [[self alloc] init]; \
} \
return singletonInstance; \
} \
} \

#ifdef DEBUG
#define DECLARE_SINGLETON DECLARE_SINGLETON_A
#else
#define DECLARE_SINGLETON DECLARE_SINGLETON_B
#endif

typedef enum{
    FFLoginTypeMobile = 0,
    FFLoginTypeQQ,
    FFLoginTypeWebChat,
    FFLoginTypeSina,
    FFLoginTypeCode,
    FFLoginTypeTouchToken,
}FFLoginType;

